this.BX=this.BX||{};this.BX.Landing=this.BX.Landing||{};this.BX.Landing.UI=this.BX.Landing.UI||{};(function(e,t,n,i,r,a,s,o,l,u,c,d,g,m,h){"use strict";var p,f,v,b;function L(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);t&&(i=i.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,i)}return n}function y(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?L(Object(n),!0).forEach((function(t){babelHelpers.defineProperty(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):L(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}var C=function(e){babelHelpers.inherits(t,e);function t(e){var i;babelHelpers.classCallCheck(this,t);i=babelHelpers.possibleConstructorReturn(this,babelHelpers.getPrototypeOf(t).call(this,e));i.setEventNamespace("BX.Landing.UI.Field.AgreementsList");i.onSelectAgreementClick=i.onSelectAgreementClick.bind(babelHelpers.assertThisInitialized(i));i.onCreateAgreementClick=i.onCreateAgreementClick.bind(babelHelpers.assertThisInitialized(i));i.onUserConsentEditSave=i.onUserConsentEditSave.bind(babelHelpers.assertThisInitialized(i));i.onUserConsentEditCancel=i.onUserConsentEditCancel.bind(babelHelpers.assertThisInitialized(i));i.onItemRemoveClick=i.onItemRemoveClick.bind(babelHelpers.assertThisInitialized(i));i.onDragEnd=i.onDragEnd.bind(babelHelpers.assertThisInitialized(i));i.items=[];n.Dom.replace(i.input,i.getListContainer());n.Dom.append(i.getActionsContainer(),i.layout);void i.showAgreementLoader();l.FormClient.getInstance().prepareOptions(i.options.formOptions,i.options.value).then((function(e){return e.data.agreements.map((function(e,t){return n.Runtime.merge(e,i.options.value[t])}))})).then((function(e){void i.hideAgreementLoader();e.forEach((function(e){i.addItem(e)}))}));i.draggable=new a.Draggable({context:window.parent,container:i.getListContainer(),draggable:".landing-ui-component-list-item",dragElement:".landing-ui-button-icon-drag",type:a.Draggable.MOVE,offset:{y:-62}});i.draggable.subscribe("end",i.onDragEnd);var r=n.Reflection.getClass("top.BX.addCustomEvent");r(window.top,"main-user-consent-to-list",i.onUserConsentEditCancel);r(window.top,"main-user-consent-saved",i.onUserConsentEditSave);return i}babelHelpers.createClass(t,[{key:"getAgreementsList",value:function e(){var t=this;return this.cache.remember("agreementsList",(function(){return t.options.agreementsList}))}},{key:"setAgreementsList",value:function e(t){this.cache.set("agreementsList",t)}},{key:"loadAgreementsList",value:function e(){return m.Backend.getInstance().action("Form::getAgreements").then((function(e){return n.Runtime.orderBy(e,["id"],["asc"])}))}},{key:"getAgreementById",value:function e(t){return this.getAgreementsList().find((function(e){return String(t)===String(e.id)}))}},{key:"addItem",value:function e(t){var n=this.createItem(t);n.appendTo(this.getListContainer());this.items=this.items.filter((function(e){return String(e.options.id)!==String(n.options.id)}));this.items.push(n)}},{key:"getListContainer",value:function e(){return this.cache.remember("listContainer",(function(){return n.Tag.render(p||(p=babelHelpers.taggedTemplateLiteral(['<div class="landing-ui-field-agreements-list-container"></div>'])))}))}},{key:"getActionsContainer",value:function e(){var t=this;return this.cache.remember("actionsContainer",(function(){return n.Tag.render(f||(f=babelHelpers.taggedTemplateLiteral(['\n\t\t\t\t<div class="landing-ui-field-agreements-list-actions-container">\n\t\t\t\t\t',"\n\t\t\t\t\t","\n\t\t\t\t</div>\n\t\t\t"])),t.getSelectAgreementButton(),t.getCreateAgreementButton())}))}},{key:"getSelectAgreementButton",value:function e(){var t=this;return this.cache.remember("selectAgreementButton",(function(){return n.Tag.render(v||(v=babelHelpers.taggedTemplateLiteral(['\n\t\t\t\t<span class="landing-ui-field-agreements-list-actions-button" onclick="','">\n\t\t\t\t\t',"\n\t\t\t\t</span>\n\t\t\t"])),t.onSelectAgreementClick,n.Loc.getMessage("LANDING_AGREEMENT_LIST_SELECT_BUTTON_LABEL"))}))}},{key:"getCreateAgreementButton",value:function e(){var t=this;return this.cache.remember("createAgreementButton",(function(){return n.Tag.render(b||(b=babelHelpers.taggedTemplateLiteral(['\n\t\t\t\t<span class="landing-ui-field-agreements-list-actions-button" onclick="','">\n\t\t\t\t\t',"\n\t\t\t\t</span>\n\t\t\t"])),t.onCreateAgreementClick,n.Loc.getMessage("LANDING_AGREEMENT_LIST_CREATE_BUTTON_LABEL"))}))}},{key:"getSelectedAgreements",value:function e(){return babelHelpers.toConsumableArray(this.getListContainer().children).map((function(e){return n.Dom.attr(e,"data-value")}))}},{key:"getAgreementsMenu",value:function e(){var t=this;return this.cache.remember("agreementsMenu",(function(){var e=new i.Menu({bindElement:t.getSelectAgreementButton(),autoHide:true,maxWidth:400,maxHeight:205,events:{onPopupShow:function t(){setTimeout((function(){n.Dom.style(e.getMenuContainer(),{left:"0px",right:"auto",top:"30px"})}))}}});t.getAgreementsList().filter((function(e){return!t.items.some((function(t){return String(t.options.id)===String(e.id)}))})).forEach((function(n){e.addMenuItem({id:n.id,text:n.name,onclick:t.onAgreementsMenuItemClick.bind(t,n)})}));n.Dom.append(e.getMenuContainer(),t.getActionsContainer());return e}))}},{key:"refreshAgreementsMenu",value:function e(){var t=this.getAgreementsMenu();t.close();t.destroy();this.cache["delete"]("agreementsMenu")}},{key:"createItemForm",value:function e(t){var i=this;return new o.FormSettingsForm({id:t.id,title:n.Loc.getMessage("LANDING_AGREEMENT_FORM_TITLE"),onChange:function e(){i.emit("onChange",{skipPrepare:true})},serializeModifier:function e(t){if(t.type==="type1"){return{checked:true,required:true}}if(t.type==="type2"){return{checked:false,required:true}}if(t.type==="type3"){return{checked:true,required:false}}if(t.type==="type4"){return{checked:false,required:false}}},fields:[new s.RadioButtonField({selector:"type",value:function(){if(t.checked===true&&t.required===true){return"type1"}if(t.checked===false&&t.required===true){return"type2"}if(t.checked===true&&t.required===false){return"type3"}if(t.checked===false&&t.required===false){return"type4"}}(),items:[{id:"type1",title:n.Loc.getMessage("LANDING_AGREEMENT_FORM_TYPE_FIELD_ITEM_1"),icon:"landing-ui-agreement-type-1-icon"},{id:"type2",title:n.Loc.getMessage("LANDING_AGREEMENT_FORM_TYPE_FIELD_ITEM_2"),icon:"landing-ui-agreement-type-2-icon"},{id:"type3",title:n.Loc.getMessage("LANDING_AGREEMENT_FORM_TYPE_FIELD_ITEM_3"),icon:"landing-ui-agreement-type-3-icon"},{id:"type4",title:n.Loc.getMessage("LANDING_AGREEMENT_FORM_TYPE_FIELD_ITEM_4"),icon:"landing-ui-agreement-type-4-icon"}]}),new c.ActionPanel({left:[{id:"edit",text:n.Loc.getMessage("LANDING_AGREEMENT_EDIT_BUTTON_LABEL"),onClick:function e(){return i.editAgreement(t)}},{id:"list",text:n.Loc.getMessage("LANDING_AGREEMENT_CONSENTS_BUTTON_LABEL"),onClick:function e(){return i.openConsentsList(t)}}]})]})}},{key:"getAgreementLoader",value:function e(){return this.cache.remember("agreementLoader",(function(){return new g.Loader({size:50,mode:"inline",offset:{top:"5px",left:"225px"}})}))}},{key:"showAgreementLoader",value:function e(){var t=this.getAgreementLoader();var i=this.getListContainer();n.Dom.append(t.layout,i);return t.show(i)}},{key:"hideAgreementLoader",value:function e(){var t=this.getAgreementLoader();n.Dom.remove(t.layout);return t.hide()}},{key:"onAgreementsMenuItemClick",value:function e(t){var n=this;void this.showAgreementLoader();l.FormClient.getInstance().prepareOptions(this.options.formOptions,{agreements:[{id:t.id}]}).then((function(e){void n.hideAgreementLoader();n.addItem(e.data.agreements[0]);n.emit("onChange",{skipPrepare:true})}));this.refreshAgreementsMenu()}},{key:"onSelectAgreementClick",value:function e(t){t.preventDefault();var n=this.getAgreementsMenu();if(!n.getPopupWindow().isShown()){n.show()}else{n.close()}}},{key:"onCreateAgreementClick",value:function e(t){t.preventDefault();this.editAgreement({id:0})}},{key:"onItemHeaderClick",value:function e(t,i){i.preventDefault();var r=i.currentTarget.parentElement;n.Dom.toggleClass(r,"landing-ui-field-agreements-list-item-active")}},{key:"createItem",value:function e(t){var n=this.getAgreementById(t.id);return new u.ListItem({id:t.id,title:n.name,description:n.labelText,sourceOptions:t,draggable:true,editable:true,removable:true,form:this.createItemForm(t),onRemove:this.onItemRemoveClick})}},{key:"setCurrentlyEdited",value:function e(t){this.cache.set("setCurrentlyEdited",t)}},{key:"getCurrentlyEdited",value:function e(){return this.cache.get("setCurrentlyEdited")||null}},{key:"buildEditPath",value:function e(t){return"/settings/configs/userconsent/edit/".concat(t,"/")}},{key:"buildConsentsListPath",value:function e(t){return"/settings/configs/userconsent/consents/".concat(t,"/")}},{key:"editAgreement",value:function e(t){this.setCurrentlyEdited(t);var n=this.buildEditPath(t.id);BX.SidePanel.Instance.open(n,{cacheable:false,allowChangeHistory:false})}},{key:"closeEditAgreementSlider",value:function e(){var t=this.getCurrentlyEdited();if(n.Type.isPlainObject(t)){var i=this.buildEditPath(t.id);var r=BX.SidePanel.Instance.getSlider(i);if(r){r.close()}}}},{key:"openConsentsList",value:function e(t){var n=this.buildConsentsListPath(t.id);BX.SidePanel.Instance.open(n,{cacheable:false,allowChangeHistory:false})}},{key:"onUserConsentEditCancel",value:function e(){this.closeEditAgreementSlider()}},{key:"onUserConsentEditSave",value:function e(){var t=this;this.closeEditAgreementSlider();void this.showAgreementLoader();var i=this.getValue();this.loadAgreementsList().then((function(e){t.setAgreementsList(e);h.FormSettingsPanel.getInstance().setAgreements(e);var r=t.getCurrentlyEdited();if(r&&r.id===0){var a=babelHelpers.toConsumableArray(e).pop();l.FormClient.getInstance().prepareOptions(t.options.formOptions,{agreements:[a]}).then((function(e){void t.hideAgreementLoader();t.addItem(e.data.agreements[0]);t.refreshAgreementsMenu();t.emit("onChange",{skipPrepare:true})}))}else{n.Dom.clean(t.getListContainer());void t.showAgreementLoader();l.FormClient.getInstance().prepareOptions(t.options.formOptions,{agreements:i}).then((function(e){void t.hideAgreementLoader();t.items=[];i.forEach((function(n){var i=e.data.agreements.find((function(e){return String(e.id)===String(n.id)}));if(i){t.addItem(y(y({},i),{},{checked:n.checked,required:n.required}))}else{t.addItem(n)}}));t.refreshAgreementsMenu();t.emit("onChange",{skipPrepare:true})}))}}))}},{key:"onItemRemoveClick",value:function e(t){var n=t.getTarget().getValue();this.items=this.items.filter((function(e){return String(e.options.id)!==String(n.id)}));this.refreshAgreementsMenu();this.emit("onItemRemove",{item:n});this.emit("onChange",{skipPrepare:true})}},{key:"onDragEnd",value:function e(){var t=this;var i=this.items;this.items=[];babelHelpers.toConsumableArray(this.getListContainer().children).forEach((function(e){var r=n.Dom.attr(e,"data-id");var a=i.find((function(e){return String(e.options.id)===String(r)}));if(a){t.items.push(a)}}));this.emit("onChange",{skipPrepare:true})}},{key:"getValue",value:function e(){return this.items.map((function(e){return e.getValue()}))}}]);return t}(r.BaseField);e.AgreementsList=C})(this.BX.Landing.UI.Field=this.BX.Landing.UI.Field||{},BX,BX,BX.Main,BX.Landing.UI.Field,BX.UI.DragAndDrop,BX.Landing.UI.Field,BX.Landing.UI.Form,BX.Crm.Form,BX.Landing.UI.Component,BX.Landing.UI.Component,BX.Event,BX,BX.Landing,BX.Landing.UI.Panel);
//# sourceMappingURL=agreementslist.bundle.map.js